# =========================================================
# Agent Packing Script (Windows PowerShell)
# Function: Pack agent project to agent.tar.gz
# Usage: .\pack_agent.ps1
# =========================================================

param(
    [string]$OutputFile = "agent.tar.gz"
)

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Agent Project Packing" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Get script directory
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ScriptDir

# Check required files
if (-not (Test-Path "main.py")) {
    Write-Host "Error: main.py not found!" -ForegroundColor Red
    Write-Host "Please run this script in the agent directory" -ForegroundColor Yellow
    exit 1
}

# Remove old archive
if (Test-Path $OutputFile) {
    Write-Host "Removing old archive..." -ForegroundColor Yellow
    Remove-Item $OutputFile -Force
}

# Check if tar is installed (Windows 10 1803+)
$tarExists = Get-Command tar -ErrorAction SilentlyContinue

if ($tarExists) {
    # Use Windows built-in tar command
    Write-Host "Packing with tar..." -ForegroundColor Blue
    
    # Pack files using exclude patterns (simpler and avoids command line length limits)
    & tar -czf $OutputFile `
        --exclude=__pycache__ `
        --exclude=*.pyc `
        --exclude=*.pyo `
        --exclude=logs `
        --exclude=screenshots `
        --exclude=.git `
        --exclude=.gitignore `
        --exclude=.idea `
        --exclude=.vscode `
        --exclude=.venv `
        --exclude=venv `
        --exclude=env `
        --exclude=.env.local `
        --exclude=node_modules `
        --exclude=.DS_Store `
        --exclude=Thumbs.db `
        --exclude=*.log `
        --exclude=state.json `
        --exclude=agent.tar.gz `
        --exclude=agent.zip `
        --exclude=pack_agent.sh `
        --exclude=pack_agent.ps1 `
        --exclude=pack_simple.ps1 `
        --exclude=pack_en.ps1 `
        *
    
    if ($LASTEXITCODE -eq 0) {
        $fileSize = (Get-Item $OutputFile).Length
        $fileSizeMB = [math]::Round($fileSize / 1MB, 2)
        
        Write-Host ""
        Write-Host "Success!" -ForegroundColor Green
        Write-Host "  File: $OutputFile" -ForegroundColor Green
        Write-Host "  Size: $fileSizeMB MB" -ForegroundColor Green
        Write-Host ""
        Write-Host "========================================" -ForegroundColor Cyan
        Write-Host "Next Steps:" -ForegroundColor Yellow
        Write-Host ""
        Write-Host "1. Upload to control server:" -ForegroundColor Blue
        Write-Host "   scp $OutputFile root@your-server:/tmp/" -ForegroundColor Gray
        Write-Host ""
        Write-Host "2. Deploy via admin panel:" -ForegroundColor Blue
        Write-Host "   - Go to [Server Management] menu" -ForegroundColor Gray
        Write-Host "   - Select servers to deploy" -ForegroundColor Gray
        Write-Host "   - Click [Batch Deploy Agent] button" -ForegroundColor Gray
        Write-Host "   - Enter file path: /tmp/$OutputFile" -ForegroundColor Gray
        Write-Host "   - Start deployment" -ForegroundColor Gray
        Write-Host ""
        Write-Host "========================================" -ForegroundColor Cyan
    } else {
        Write-Host "Packing failed!" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "Error: tar command not found!" -ForegroundColor Red
    Write-Host "Please use Windows 10 1803+ or install tar manually" -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Alternative: Use WSL or Git Bash to run pack_agent.sh" -ForegroundColor Yellow
    exit 1
}

